<html>
<head>
    <meta charset="UTF-8">
    <title>应用模型信息列表</title>
    <link id="easyuiTheme" href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
    <script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
    <script src="../../ffres/dict/all.js" type="text/javascript"></script>
    <link href="../../static/easyui/themes/frameless.css" rel="stylesheet" type="text/css"/>
    <a href="" target="_blank" id="commonLink" name="commonLink"></a>
    <style type="text/css">
        .tba,.tba th,.tba td{text-align:center;border: 1px solid #bcc1c2;border-collapse:collapse;}
        .tba th{font-weight:normal;font-size:15px;background-color: #5173a0;color:white}
        .tba th,.tba td{height:30px;min-width:70px;overflow:hidden;white-space:nowrap;}
        .tba td{font-size:14px;}
        .tba{width:100%;margin: 0 auto;border:none;}
        .tb th,.tb td{height:30px;min-width:100px;overflow:hidden;white-space:nowrap;}
        .selected{background-color: #cde4fc;}
    </style>
</head>
<script type="text/javascript">
    var appId = $.getUrlParam('appId');
    var packageName = $.getUrlParam('packageName');
    //服务单元前一选择ID
    var serviceUnitPreviousID;

    //表模型前一选择ID
    var tableModelInfoPreviousID;

    //空白单元前选择ID
    var SelectIDBeforeBlankCell;

    var Blank;

    function doAdd() {
        if (tableModelInfoPreviousID){
            var urlParameter=tableModelInfoPreviousID.split("_1_");
            JLEUtil.createModalWin('新增数据服务模型', `AppModelInfoAdd-I01.html?appId=${appId}&tableModelId=${urlParameter[0]}&tableModelName=${urlParameter[1]}&tableModelCode=${urlParameter[2]}&type=2`);
        }else {
            JLEUtil.createModalWin('新增数据服务模型', `AppModelInfoAdd-I01.html?appId=${appId}&type=1`);
        }
    }
    function doAddNoServiceUnit() {
        if (SelectIDBeforeBlankCell) {
            var urlParameter= SelectIDBeforeBlankCell.split("_1_")
            JLEUtil.createModalWin('修改数据服务模型', `AppModelInfoAdd-I01.html?appId=${appId}&tableModelId=${urlParameter[0]}&tableModelName=${urlParameter[1]}&tableModelCode=${urlParameter[2]}&type=2`);
        } else {
            $.messager.alert('提示', '请点击选择一个服务单元！', 'info');
        }
    }

    function doEditServiceUnit() {
        if (serviceUnitPreviousID) {
            var urlParameter= serviceUnitPreviousID.split("_1_")
            var url = `AppModelInfoUpdate-I01.html?appId=${appId}&appModelId=${urlParameter[0]}&appModelType=${urlParameter[1]}&appModelName=${urlParameter[2]}&appModelTypeName=${urlParameter[3]}&packageName=${packageName}`
            JLUtil.linkNewPage(url);
        } else {
            $.messager.alert('提示', '请点击选择一个服务单元！', 'info');
        }
    }

    function doEditTableModelInfo(){
        if (tableModelInfoPreviousID) {
            var urlParameter=tableModelInfoPreviousID.split("_1_");
            var objectId = urlParameter[0];
            var objectName = urlParameter[1];
            var url = "ModelTableFieldList.html?objectId=" + objectId + "&objectName=" + objectName;
            parent.addTab(objectName, '../esaas-dmcenter/' + url);
        } else {
            $.messager.alert('提示', '请点击选择一个领域单元！', 'info');
        }
    }

    function doDelete() {
        if (serviceUnitPreviousID) {
            var urlParameter= serviceUnitPreviousID.split("_1_")
            $.messager.confirm('删除提示', '你确定要删除这条记录?', function (r) {
                if (r) {
                    var jsonData = JSON.parse('{' + '"appModelId":"' + urlParameter[0] + '"' + '}');
                    $.restfulDelete('../../api/app/model/info', jsonData, function (data) {
                        $.messager.alert('提示', '记录删除成功!', 'info');
                        reloadList();
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请点击选择一个服务单元！', 'info');
        }
    }

    function doView() {
        if (serviceUnitPreviousID) {
            var urlParameter= serviceUnitPreviousID.split("_1_")
            var url = `AppModelInfoUpdate-I01.html?appId=${appId}&appModelId=${urlParameter[0]}&appModelType=${urlParameter[1]}&readonly=true`
            JLUtil.linkNewPage(url);
        } else {
            $.messager.alert('提示', '请点击选择一个服务单元！', 'info');
        }
    }

    function getData(){
        $("#dataPanel").empty();
        var jsss= $("#searchForm").serializeJson();
        var jsonData= JSON.parse('{'+'"tableModelName":"'+jsss.tableModelName+'"' + ','+'"tableModelCode":"'+jsss.tableModelCode+'"'+','+'"appId":"'+appId+'"'+'}');
        $.restfulPostJson("../../api/app/model/infos2",jsonData,function (data){
            createAppModelInfoHtml(data.rows)
        })
    }
    function createAppModelInfoHtml(data){
        if(data.length==0){
            var tableHtml="";
            tableHtml+="<div style='float: left;width: 100%;padding:4px;' ><h1>暂无数据</h1></div>";
            $("#dataPanel").append(tableHtml);
        }else{
            for(var i=0;i<data.length;i++){
                var tableHtml="";
                var sub=data[i];
                var appModelType;
                var appModelId;
                var appModelTypeName;
                var rowspan=5;
                if (sub.appModelType&&sub.appModelId){
                    appModelType= sub.appModelType.split(",")
                    appModelId= sub.appModelId.split(",")
                    appModelTypeName = sub.appModelTypeName.split(",")
                    if (appModelType.length>=8)
                        rowspan = (appModelType.length/2)+2;
                }
                tableHtml+="<div style='float: left;width: 31%; min-width:300px;padding:10px;' ><table class='tba'>\n" +
                    "                <thead>\n" +
                    "                <tr>\n" +
                    "                    <th>领域模型</th>\n" +
                    "                    <th colspan='2' >领域服务</th>\n" +
                    "                </tr>\n" +
                    "                </thead>\n" +
                    "                <tbody>"

                tableHtml+="<tr><td rowspan='"+rowspan+"' id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"' onclick='selectTableModelInfo(this.id)' ondblclick='doEditTableModelInfo()'>"+sub.tableModelName+"<br>"+sub.tableModelCode+"</td>";
                if(sub.appModelType&&sub.appModelId){
                    for (let j = 0; j < appModelType.length; j=j+2) {
                        if (appModelType[j+1]!=null)
                            tableHtml+="<tr><td id='"+appModelId[j]+"_1_"+appModelType[j]+"_1_"+sub.tableModelName+"_1_"+appModelTypeName[j]+"'onclick='selectServiceUnit(this.id)' ondblclick='doEditServiceUnit()'>"+appModelTypeName[j]+"</td>" +
                                       "<td id='"+appModelId[j+1]+"_1_"+appModelType[j+1]+"_1_"+sub.tableModelName+"_1_"+appModelTypeName[j+1]+"'onclick='selectServiceUnit(this.id)' ondblclick='doEditServiceUnit()'>"+appModelTypeName[j+1]+"</td></tr>";
                        else
                            tableHtml+="<tr><td id='"+appModelId[j]+"_1_"+appModelType[j]+"_1_"+sub.tableModelName+"_1_"+appModelTypeName[j]+"'onclick='selectServiceUnit(this.id)' ondblclick='doEditServiceUnit()'>"+appModelTypeName[j]+"</td>" +
                                       "<td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+(j+1)+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()'></td></tr>";
                    }
                    if (rowspan >5 && (appModelType.length%2)==0){
                        tableHtml+="<tr><td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+10+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()' ></td>" +
                            "<td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+11+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()' ></td></tr>";
                    }
                    if (8-appModelType.length>=2){
                        for (let j = 1; j < 8-appModelType.length; j=j+2) {
                            tableHtml+="<tr><td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+(j+10)+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()' ></td>" +
                                       "<td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+(j+11)+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()' ></td></tr>";
                        }
                    }
                }else {
                    for (let j = 0; j < 4; j++) {
                        tableHtml+="<tr><td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+(j+10)+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()' ></td>" +
                                   "<td id='"+sub.tableModelId+"_1_"+sub.tableModelName+"_1_"+sub.tableModelCode+"_1_"+(j+21)+"' onclick='selectBlankCell(this.id)' ondblclick='doAddNoServiceUnit()' ></td></tr>";
                    }
                }
                tableHtml+="</tr></tbody>\n" +
                    "            </table></div>";
                $("#dataPanel").append(tableHtml);
            }
        }
    }


    function selectServiceUnit(tid) {
        if (tableModelInfoPreviousID!=null){}
            $("#"+tableModelInfoPreviousID).removeClass("selected");
        if (serviceUnitPreviousID!=null)
            $("#"+serviceUnitPreviousID).removeClass("selected");
        if (SelectIDBeforeBlankCell!=null)
            $("#"+SelectIDBeforeBlankCell).removeClass("selected");
        $("#"+tid).addClass("selected");
        tableModelInfoPreviousID=Blank;
        SelectIDBeforeBlankCell=Blank;
        serviceUnitPreviousID = tid;
        console.log(tid)
    }

    function selectBlankCell(tid) {
        if (tableModelInfoPreviousID!=null)
            $("#"+tableModelInfoPreviousID).removeClass("selected");
        if (serviceUnitPreviousID!=null)
            $("#"+serviceUnitPreviousID).removeClass("selected");
        if (SelectIDBeforeBlankCell!=null)
            $("#"+SelectIDBeforeBlankCell).removeClass("selected");
        $("#"+tid).addClass("selected");
        serviceUnitPreviousID=Blank;
        tableModelInfoPreviousID=Blank;
        SelectIDBeforeBlankCell = tid;
    }

    function selectTableModelInfo(tid) {
        if (tableModelInfoPreviousID!=null)
            $("#"+tableModelInfoPreviousID).removeClass("selected");
        if (serviceUnitPreviousID!=null)
            $("#"+serviceUnitPreviousID).removeClass("selected");
        if (SelectIDBeforeBlankCell!=null)
            $("#"+SelectIDBeforeBlankCell).removeClass("selected");
        $("#"+tid).addClass("selected");
        serviceUnitPreviousID=Blank;
        SelectIDBeforeBlankCell=Blank;
        tableModelInfoPreviousID = tid;
    }

    function reloadList() {
        getData();
    };
    $(function () {
        getData();
    });
</script>
</head>
<body class="easyui-layout">
    <div data-options="region:'north',title:''" style="height: 40px; background: #F4F4F4;overflow: hidden">
        <form id="searchForm">
            <table>
                <tr style="float: left">
                    <td >&nbsp;领域模型代码：</td>
                    <td ><input name="tableModelCode" id="tableModelCode" /></td>
                    <td >&nbsp;领域模型名称：</td>
                    <td ><input name="tableModelName" id="tableModelName" /></td>
                    <td ><a class="easyui-linkbutton" href="javascript:void(0);"
                            onclick="getData()">查找</a>
                    </td>
                    <td ><a class="easyui-linkbutton" href="javascript:void(0);"
                            onclick="JLEUtil.clearForm('searchForm');">清空</a></td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',split:false">
        <table id="dg" class="easyui-datagrid" title=""
            data-options="singleSelect:true,collapsible:true" toolbar="#tb">
        </table>
            <div id="dataPanel" style="float: left;width: 100%" >
            </div>
        </div>
    <div id="tb" style="padding:1px">
        <div style="margin-bottom:2px">
            <a href="javascript:doAdd()" class="easyui-linkbutton" iconCls="icon-add" plain="true">创建领域服务</a>
            <a href="javascript:doEditServiceUnit()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">编辑领域服务</a>
            <a href="javascript:doDelete()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除领域服务</a>
            <a href="javascript:doView()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">查看领域服务</a>
        </div>
    </div>
</body>
</html>